// socketStore.ts
import { th } from "element-plus/es/locales.mjs";
import { defineStore } from "pinia";
import { io, Socket } from 'socket.io-client';
import { ref } from 'vue';

interface Message {
    id:string,
    author: string,
    content: string,
    type:string,
}

export const useSocketStore = defineStore("chat", () => {
    const roomId = ref<string>('')
    const messages = ref<Message[]>([]);
    const uniqueId = ref<string>('');
    const called = ref<boolean>(false);// 是否是接收方
    const caller = ref<boolean>(false);// 是否是发起方
    const inRoom = ref<boolean>(false);// 是否加入房间
    const calling = ref<boolean>(false);// 呼叫中
    const communicating = ref<boolean>(false);// 视频通话中
    // const socket = ref<Socket>();
    const username = ref<string>(''); // 保存客户端的用户名


    const socket: Socket = io('http://localhost:3000');

    socket.on('message', (message: Message) => {
        messages.value.push(message);
    })

    

    return {
        messages, called, caller, calling, inRoom,
        communicating, socket, roomId, uniqueId
    };
});